// xHamster.js for PSPTube
// Made By JK108

function xHamster_CheckURL( url, option )
{
	if (url.match("http://xhamster.com") == "http://xhamster.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a xHamster URL
*/
function xHamster_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var file = contents.match(/so.addVariable\(\'file\',\'(.*?)\'/);
	var file = file[1];
	var server = contents.match(/so.addVariable\(\'srv\',\'(.*?)\'/);
	var server = server[1];
	return "http://dl" + server + ".xhamster.com/flv2/" + file;
}

function xHamster_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 28;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://xhamster.com/search.php?q=" + keyword.replace(/ /g, "+") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://xhamster.com/search.php?q=" + keyword.replace(/ /g, "+") + "&page=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("<span class=\"error\">No results for this query</span>") == "<span class=\"error\">No results for this query</span>"){ result.total = 0; return result; }
	var pos = contents.indexOf("</A></SPAN>&nbsp;&nbsp;<SPAN class=navNext>");
	var total = contents.substring(0, pos);
	pos = total.lastIndexOf(">");
	total = total.substring(pos + 1, total.length);
	var total = parseInt(total.replace(/,/g, "")) * perpage;
	result.total = total;


	while(1){
	pos = contents.indexOf("valign=top><a href=\"");

	if(pos != -1){
		contents = contents.substring(pos + 11, contents.length);
		//Get URL
		var url = contents.match(/href\=\"(.*?)\"/);
		var url = "http://xhamster.com" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/this.src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/\<div class\=moduleFeaturedTitle\>(.*?)\<\/div\>/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/\<DIV class\=moduleFeaturedDetails\>Runtime\: (.*?)\<BR\>/);
		var length = TimestringToSeconds(length[1].replace(/m/g, ":").replace(/s/g, ""));
		//Description
		var description = splitTo2Lines(title, 60);
		//Views
		var views = contents.match(/\<BR\>Views\: (.*?)\<\/DIV\>/);
		var views = parseInt(views[1].replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var xHamster = new Object();
xHamster.Name          = "xHamster";		/* Service name */
xHamster.Description   = "xHamster";		/* Service description */
xHamster.SearchDesc    = "xHamster";		/* Service search screen description */
xHamster.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
xHamster.CheckURL      = xHamster_CheckURL;	/* CheckURL function */
xHamster.GetURL        = xHamster_GetURL;		/* GetURL function */
xHamster.Search        = xHamster_Search;		/* Search function */

// add xHamster to site list
SiteList.push( xHamster );
